﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

public partial class addrecordlist : System.Web.UI.Page
{
    string UID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        TextBox3.Text = Session["id"].ToString();
        if (!IsPostBack)
        {
            this.bind();
        }
    }

    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select GID,('('+GBrand+')'+GCHName+'('+GXinhao+')') AS DisplayName from Goods where  GCHName like '%" + TextBox1.Text + "%' or GXinhao like '%" + TextBox1.Text + "%' ";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        ListBox1.DataSource = myDs;
        ListBox1.DataTextField = "DisplayName";
        ListBox1.DataBind();
        myDa.Dispose();
        myDs.Dispose();
        myConn.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where ('('+GBrand+')'+GCHName+'('+GXinhao+')') like '%" + ListBox1.SelectedValue.ToString() + "%'";
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();
        string sqlll = "select GXinhao from Goods where GID='"+ GID +"'";
        SqlCommand comm = new SqlCommand(sqlll, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader readd = comm.ExecuteReader();
        readd.Read();
        string GX = readd["GXinhao"].ToString();
        readd.Close();
        myConn.Close();
        DropDownList1.SelectedValue = GX;
    }


    public void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods";
        SqlDataAdapter myDa = new SqlDataAdapter(sql, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        DropDownList1.DataSource = myDs;
        DropDownList1.DataTextField = "GXinhao";
        DropDownList1.DataBind();
        myDa.Dispose();
        myDs.Dispose();

        string sql1 = "select (bianhao+PName) as Pname from Projects";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql1, myConn);
        DataSet sqlDs = new DataSet();
        sqlDa.Fill(sqlDs);
        DropDownList3.DataSource = sqlDs;
        DropDownList3.DataTextField = "Pname";
        DropDownList3.DataBind();
        sqlDa.Dispose();
        sqlDs.Dispose();

        string sqll = "select UID from Users where UName='" + Session["name"].ToString() + "'";
        SqlCommand com = new SqlCommand(sqll, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        UID = read["UID"].ToString();
        read.Close();
        TextBox3.Text = UID;
        myConn.Close();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where ('('+GBrand+')'+GCHName+'('+GXinhao+')') like '%" + ListBox1.SelectedValue.ToString() + "%'";
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();
        string sqlll ="select * from Store where SGID='"+ GID +"'and SWID='"+ DropDownList2.SelectedValue +"'";
        SqlCommand comm = new SqlCommand(sqlll, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader readd = comm.ExecuteReader();
        if (readd.Read())
        {
            Label5.Text = readd[4].ToString();
            Label7.Text = readd[2].ToString();
            Label9.Text = Label5.Text;
            Label11.Text = Convert.ToString(Convert.ToInt32(Label7.Text) - Convert.ToInt32(Label5.Text));
        }
        else
        {
            Label5.Text = "0";
            Label7.Text = "500";
            Label9.Text = "0";
            Label11.Text = "500";
        }
        readd.Close();
        myConn.Close();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where GXinhao='"+ DropDownList1.SelectedValue +"'";
        SqlCommand comm = new SqlCommand(sql, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader read = comm.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();

        string sqlpro = "select * from Projects where (bianhao+PName)='" + DropDownList3.SelectedValue + "'";
        SqlCommand mycom = new SqlCommand(sqlpro, myConn);
        mycom.ExecuteNonQuery();
        SqlDataReader readpro = mycom.ExecuteReader();
        readpro.Read();
        string PID = readpro["PID"].ToString();
        readpro.Close();

        string logname;
        string sqllname = "select UID from Users where UName='" + Session["name"].ToString() + "'";
        SqlCommand comname = new SqlCommand(sqllname, myConn);
        comname.ExecuteNonQuery();
        SqlDataReader readname = comname.ExecuteReader();
        readname.Read();
        string UID = readname["UID"].ToString();
        readname.Close();
        logname = UID;


        DateTime dt = DateTime.Now;
        bool type = true;
        string sqll;

        string sqlll;

        string sqllll = "select * from Store where SGID='"+ GID +"'and SWID='"+ DropDownList2.SelectedValue +"'";
        SqlCommand com1 = new SqlCommand(sqllll, myConn);
        SqlDataReader dr = com1.ExecuteReader();
        if (TextBox2.Text.Trim().ToString() != "" && DropDownList3.SelectedValue != "" && TextBox3.Text.Trim().ToString() != "")
        {
            if (dr.Read())
            {
                dr.Close();
                sqll = "insert into RecordsIN (RGID,RWID,RQuantity,RType,RManager,RHandler,RDate,RifAOG,RPID) values('" + GID + "','" + DropDownList2.SelectedValue + "','" + Convert.ToDouble(TextBox2.Text.Trim()) + "','" + type + "','" + logname + "','" + TextBox3.Text.Trim().ToString() + "','" + dt + "','未到货','" + PID + "')";
                SqlCommand com = new SqlCommand(sqll, myConn);
                com.ExecuteNonQuery();
                myConn.Close();
                Response.Write("<script>alert('添加成功!')</script>");
            }
            else
            {
                dr.Close();
                sqlll = "insert into Store (SGID,SWID,STopLine,SbaseLine,SQuantity) values('" + GID + "','"+ DropDownList2.SelectedValue +"','500','100','0')";
                sqll = "insert into RecordsIN (RGID,RWID,RQuantity,RType,RManager,RHandler,RDate,RifAOG,RPID) values('" + GID + "','" + DropDownList2.SelectedValue + "','" + Convert.ToDouble(TextBox2.Text.Trim()) + "','" + type + "','" + logname + "','" + TextBox3.Text.Trim().ToString() + "','" + dt + "','未到货','" + PID + "')";
                SqlCommand com = new SqlCommand(sqll, myConn);
                com.ExecuteNonQuery();
                com.CommandText = sqlll;
                com.ExecuteNonQuery();
                myConn.Close();
                Response.Write("<script>alert('添加成功!')</script>");
                // this.Close();
                Button5.Visible = true;
            }
        }
        else
            Response.Write("<script>alert('录入信息不符合要求!')</script>");
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sql = "select * from Goods where ('('+GBrand+')'+GCHName+'('+GXinhao+')') like '%" + ListBox1.SelectedValue.ToString() + "%'";
        SqlCommand com = new SqlCommand(sql, myConn);
        com.ExecuteNonQuery();
        SqlDataReader read = com.ExecuteReader();
        read.Read();
        string GID = read["GID"].ToString();
        read.Close();
        string sqlll = "select GXinhao from Goods where GID='" + GID + "'";
        SqlCommand comm = new SqlCommand(sqlll, myConn);
        comm.ExecuteNonQuery();
        SqlDataReader readd = comm.ExecuteReader();
        readd.Read();
        string GX = readd["GXinhao"].ToString();
        readd.Close();
        myConn.Close();
        DropDownList1.SelectedValue = GX;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        //ListBox1.ClearSelection();
        Label5.Text = "";
        Label7.Text = "";
        Label9.Text = "";
        Label11.Text = "";
        TextBox2.Text = "";
        DropDownList3.SelectedValue = DropDownList3.SelectedValue;
        TextBox3.Text = UID;
    }
}